Page({
  data: {
    orderList:[],         //订单列表
    sortedOrderList:[],   //分类好的订单列表
    sortList:[1,0,0,0],   //分类索引列表
  },
  //bindTapChangeSort 点击分类响应函数
  bindTapChangeSort(e){
    //防止点到其他的节点
    if(!e.target.dataset.index) return;
    //更新分类索引列表
    var index = e.target.dataset.index;
    var sortList = this.data.sortList;
    for(let i=0;i<sortList.length;i++) sortList[i]=0;
    sortList[index] = 1;
    this.setData({sortList:sortList});
    //开始调用分类函数
    this.sortFilter(this.data.orderList);
  },
  //sortFilter 商品分类函数
  sortFilter(list){
    //计算出是哪种分类的索引
    var sortIndex = 0;
    var newdata = list;//不是废代码！！！
    for(let value of this.data.sortList){
      if(value==1) break;
      sortIndex+=1;
    }
    //根据分类的索引进行对应的分类
    switch(sortIndex){
      case 0:
        //默认分类不做处理
        break;
      case 1:
        newdata = newdata.filter((item)=>item.statu==0);
        break;
      case 2:
        newdata = newdata.filter((item)=>item.statu==1);
        break;
      case 3:
        newdata = newdata.filter((item)=>item.statu==2);
        break;
      default:
        console.log("sortIndex Err!");
        break;
    }
    //更新过滤后的数据
    this.setData({
      sortedOrderList:newdata
    });
  },
  //getOrderData 获取订单数据函数
  getOrderData(){
    var that = this;
    var app = getApp();
    var cid = app.globalData.clientInfo.cid;
    //网络请求
    app.pinia('checkOrder',{
      cid:cid
    },(res)=>{
      let newData = res.data;
      //添加状态文字
      newData.forEach((item)=>{
        let statu = item.statu;
        switch(statu){
          case '0':
            item.statuText = '已下单';
            break;
          case '1':
            item.statuText = '已发货';
            break;
          case '2':
            item.statuText = '已确认';
            break;
          default:
            break;
        }
      });
      //更新数据
      that.setData({orderList:newData});
      that.sortFilter(this.data.orderList);
    });
  },
  //bindTapComfirmOrder 点击确认订单响应函数
  bindTapComfirmOrder(e){
    var app = getApp();
    var that = this;
    var oid = e.target.dataset.oid;
    var cid = app.globalData.clientInfo.cid;
    app.pinia('comfirmOrder',{
      oid:oid,
      cid:cid
    },(res)=>{
      console.log(res.data);
      //本地更新数据
      that.data.orderList.forEach(item=>{
        if(item.oid == oid){
          item.statu = '2';
          item.statuText = '已确认';
        }
      });
      that.sortFilter(that.data.orderList);
      //提示
      wx.showToast({
        title: '已确认',
        icon:'success'
      });
    });
  },
  //bindTapBuyOrder 点击再次购买响应函数
  bindTapBuyOrder(e){
    var gid = e.target.dataset.gid;
    (getApp()).pinia('getGoodDetail',{
      gid:gid
    },(res)=>{
      let goodData = [res.data];
      wx.navigateTo({
        url: '/pages/goodDetail/goodDetail',
        success: function(res) {
          res.eventChannel.emit('goodData', { data: goodData })
        }
      });
    });
  },
  onLoad(options) {
    (getApp()).isLogined(); //检查是否登录
    this.setData({themeIndex:(getApp()).globalData.themeIndex});//设置主题
    this.getOrderData();    //获取订单信息
  }
})